package com.ipet.user.controller;

import com.github.pagehelper.PageInfo;
import com.ipet.user.pojo.User;
import com.ipet.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


@RestController
@Api(value = "用户模块")
public class UserController {
    @Autowired
    private UserService userService;
    @ApiOperation(value = "获取所有的用户")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码" , name = "pageNo" , required = true , dataType = "Integer" , paramType = "query"),
            @ApiImplicitParam(value = "每页数据条数" , name = "pageSize" , required = true , dataType = "Integer" , paramType = "query")
    })
    @GetMapping("/usersPage")
    public ResponseEntity<PageInfo<User>> getAllUser(Integer pageNo , Integer pageSize){
        PageInfo<User> userPage = userService.findUserPage(pageNo, pageSize);
        System.out.println(userPage);
        if(userPage == null){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(userPage);
    }
    @ApiOperation(value = "根据用户名模糊查询并分页")
    @ApiImplicitParam(value = "用户名关键字" , name = "username" , required = true , dataType = "String" , paramType = "query")
    @PostMapping("/userPage")
    public ResponseEntity<PageInfo<User>> getUserByName(String username , Integer pageNo , Integer pageSize){
        PageInfo<User> userByName = userService.getUserByName(username, pageNo, pageSize);
        if(userByName == null){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(userByName);
    }
//    修改或添加用户信息 主要是后台控制此用户是否可用
    @ApiOperation(value = "添加或修改用户")
    @ApiImplicitParam(value = "user对象"  , name = "user" , required = true , dataType = "User" , paramType = "body")
    @PostMapping("/saveOrUpdate")
    public ResponseEntity<Boolean> updateUser(@RequestBody User user){
        boolean saveOrUpdate = userService.saveOrUpdate(user);
        return ResponseEntity.ok(saveOrUpdate);
    }
    @DeleteMapping("/delete")
    @ApiOperation("删除用户")
    @ApiImplicitParam(value = "要删除的用户id" , name = "id" , required = true , dataType = "Integer" , paramType = "query")
    public ResponseEntity<Boolean> deleteUser( Integer id){
        boolean remove = userService.removeById(id);
        return ResponseEntity.ok(remove);
    }
}
